"""
查询缓存管理路由
提供查询缓存的状态查看和管理功能
"""

from fastapi import APIRouter, Depends
from typing import Dict, Any
from src.services import query_cache_service, get_current_active_user
from src.models.user import User

router = APIRouter(prefix="/query-cache", tags=["查询缓存"])

@router.get("/stats")
async def get_cache_stats(current_user: User = Depends(get_current_active_user)):
    """
    获取查询缓存统计信息
    
    Returns:
        缓存统计信息
    """
    return query_cache_service.get_stats()

@router.post("/clear")
async def clear_cache(current_user: User = Depends(get_current_active_user)):
    """
    清空查询缓存
    
    Returns:
        清空结果
    """
    cleared_count = query_cache_service.cache_service.clear()
    query_cache_service.clear_stats()
    return {
        "message": "查询缓存已清空",
        "cleared_items": cleared_count
    }

@router.post("/clear-stats")
async def clear_cache_stats(current_user: User = Depends(get_current_active_user)):
    """
    清空查询缓存统计信息
    
    Returns:
        清空结果
    """
    query_cache_service.clear_stats()
    return {
        "message": "查询缓存统计信息已清空"
    }

@router.get("/health")
async def cache_health_check(current_user: User = Depends(get_current_active_user)):
    """
    检查查询缓存服务健康状态
    
    Returns:
        健康状态
    """
    is_healthy = query_cache_service.health_check()
    return {
        "status": "healthy" if is_healthy else "unhealthy",
        "healthy": is_healthy
    }